package com.example.hidesms.receiver;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.example.hidesms.bo.SmsBO;
import com.example.hidesms.dto.SMSItem;
import com.example.hidesms.sqlite.DataBaseHelper;
import com.example.hidesms.utils.Utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsFilter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                Object[] pdus = (Object[])extras.get("pdus");

                if (pdus.length < 1) return; // Invalid SMS. Not sure that it's possible.

                StringBuilder sb = new StringBuilder();
                String sender = null;
                String time = null;
                for (int i = 0; i < pdus.length; i++) {
                    SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    if (sender == null) sender = message.getOriginatingAddress();
                    String text = message.getMessageBody();
                    if (text != null) sb.append(text);
                    time = message.getTimestampMillis()+"";
                }
                DataBaseHelper myDatabase = new DataBaseHelper(context);
                SMSItem item =new SMSItem();
                item.setAddress(sender);
                item.setMsg(sb.toString());
                item.setTime(time);
                item.setTimeFormated(time);
                item.setReadState("0");
                String[] arr = Utils.getContactByNumber(sender, context);
                if (arr!=null && arr.length>1) {
                	item.setPhotoUri(arr[1]);
				}
                InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context
						.getContentResolver(), Uri.parse(item.getPhotoUri()));

				Bitmap bm = BitmapFactory.decodeStream(input);
				if (bm != null) {
					ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
					if (bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)) {
						item.setImage(byteArrayOutputStream.toByteArray());
					}
				}
                
                boolean flag = SmsBO.getInstance().insert(myDatabase, item);
                
                myDatabase.close();
                Log.e("smsfilter", "mes	" +sb);
                Log.e("smsfilter", "sen " +sender);
//                Log.e("smsfilter", "flag" +flag);
//                Log.e("smsfilter", "flag" +myDatabase);
                if (sender != null && sender.equals("MUACHUNG")) {
                    // Process our sms...
                    abortBroadcast();
                }
                return;
            }
        }

        // ...
    }
}